package com.czl.base.data.bean.tengyun;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import java.util.List;

/**
 * @author xubin
 */
public class LocatListBean extends BaseNumBean implements Parcelable {

    public static final int NUM_TYPE_STOCK = 4;
    public static final int NUM_TYPE_STORAGE = 5;

    private String storehouseLocatName;
    private Integer storehouseLcatId;
    private Integer storehouseLocatId;
    private Integer storageNum;
    private String roomId;
    //借出数量
    private Integer stockNum;
    //移位数量
    private Integer shiftNum;
    //已添加的数量
    private int addNum;
    private String shiftLocatOut;
    private List<LocatListBean> childList;
    private List<ProductBean> productList;
    private List<SortBean> sortList;

    //选择该位置
    private boolean selected;
    private int defaultNumType;
    private Integer locationStockNum;

    public LocatListBean() {
    }

    /*public static List<LocatListBean> createList(List<StoreHouseBean> list) {
        List<LocatListBean> locatListBeanList = new ArrayList<>();
        if (list != null) {
            for (StoreHouseBean bean : list) {
                LocatListBean locatListBean = new LocatListBean();
                locatListBean.setStorageNum(bean.getStorageNum());
                locatListBean.setStorehouseLocatId(bean.getStorehouseLocatId());
                locatListBeanList.add(locatListBean);
            }
        }
        return locatListBeanList;
    }*/

    protected LocatListBean(Parcel in) {
        storehouseLocatName = in.readString();
        if (in.readByte() == 0) {
            storehouseLcatId = null;
        } else {
            storehouseLcatId = in.readInt();
        }
        if (in.readByte() == 0) {
            storehouseLocatId = null;
        } else {
            storehouseLocatId = in.readInt();
        }
        if (in.readByte() == 0) {
            storageNum = null;
        } else {
            storageNum = in.readInt();
        }
        roomId = in.readString();
        if (in.readByte() == 0) {
            stockNum = null;
        } else {
            stockNum = in.readInt();
        }
        if (in.readByte() == 0) {
            shiftNum = null;
        } else {
            shiftNum = in.readInt();
        }
        addNum = in.readInt();
        shiftLocatOut = in.readString();
        childList = in.createTypedArrayList(LocatListBean.CREATOR);
        productList = in.createTypedArrayList(ProductBean.CREATOR);
        sortList = in.createTypedArrayList(SortBean.CREATOR);
        selected = in.readByte() != 0;
        defaultNumType = in.readInt();
        if (in.readByte() == 0) {
            locationStockNum = null;
        } else {
            locationStockNum = in.readInt();
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(storehouseLocatName);
        if (storehouseLcatId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(storehouseLcatId);
        }
        if (storehouseLocatId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(storehouseLocatId);
        }
        if (storageNum == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(storageNum);
        }
        dest.writeString(roomId);
        if (stockNum == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(stockNum);
        }
        if (shiftNum == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(shiftNum);
        }
        dest.writeInt(addNum);
        dest.writeString(shiftLocatOut);
        dest.writeTypedList(childList);
        dest.writeTypedList(productList);
        dest.writeTypedList(sortList);
        dest.writeByte((byte) (selected ? 1 : 0));
        dest.writeInt(defaultNumType);
        if (locationStockNum == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(locationStockNum);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<LocatListBean> CREATOR = new Creator<LocatListBean>() {
        @Override
        public LocatListBean createFromParcel(Parcel in) {
            return new LocatListBean(in);
        }

        @Override
        public LocatListBean[] newArray(int size) {
            return new LocatListBean[size];
        }
    };

    public int getDefaultNumType() {
        return defaultNumType;
    }

    public void setDefaultNumType(int defaultNumType) {
        this.defaultNumType = defaultNumType;
    }

    public int getAddNum() {
        return addNum;
    }

    public int getAddNumByType() {
        if (NUM_TYPE_STOCK == defaultNumType) {
            return stockNum == null ? 0 : stockNum;
        } else if (NUM_TYPE_STORAGE == defaultNumType) {
            return storageNum == null ? 0 : storageNum;
        }
        return addNum;
    }

    public int getAddNumByType(boolean purchase) {
        if (NUM_TYPE_STOCK == defaultNumType) {
            return stockNum == null ? 0 : stockNum;
        } else if (NUM_TYPE_STORAGE == defaultNumType) {
            if (purchase) {
                return getStockNumInt();
            }
            return storageNum;
        }
        return addNum;
    }

    public void setAddNum(int addNum) {
        this.addNum = addNum;
    }

    public String getShiftLocatOut() {
        return shiftLocatOut;
    }

    public void setShiftLocatOut(String shiftLocatOut) {
        this.shiftLocatOut = shiftLocatOut;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public Integer getShiftNum() {
        return shiftNum;
    }

    public void setShiftNum(Integer shiftNum) {
        this.shiftNum = shiftNum;
    }

    public List<SortBean> getSortList() {
        return sortList;
    }

    public void setSortList(List<SortBean> sortList) {
        this.sortList = sortList;
    }

    public List<ProductBean> getProductList() {
        return productList;
    }

    public void setProductList(List<ProductBean> productList) {
        this.productList = productList;
    }

    public List<LocatListBean> getChildList() {
        return childList;
    }

    public void setChildList(List<LocatListBean> childList) {
        this.childList = childList;
    }

    public boolean isHasChild() {
        return childList != null && !childList.isEmpty();
    }

    private List<StockListBean> stockList;

    public String getStorehouseLocatName() {
        return storehouseLocatName;
    }

    public void setStorehouseLocatName(String storehouseLocatName) {
        this.storehouseLocatName = storehouseLocatName;
    }

    public Integer getStorehouseLcatId() {
        return storehouseLcatId;
    }

    public void setStorehouseLcatId(Integer storehouseLcatId) {
        this.storehouseLcatId = storehouseLcatId;
    }

    public Integer getStorehouseLocatId() {
        return storehouseLocatId;
    }

    public void setStorehouseLocatId(Integer storehouseLocatId) {
        this.storehouseLocatId = storehouseLocatId;
    }

    public int getStorageNum() {
        return storageNum == null ? 0 : storageNum;
    }

    public void setStorageNum(int storageNum) {
        this.storageNum = storageNum;
    }

    public Integer getStockNum() {
        return stockNum;
    }

    public int getStockNumInt() {
        return stockNum == null ? 0 : stockNum;
    }

    public void setStockNum(Integer stockNum) {
        this.stockNum = stockNum;
    }

    public String getRoomId() {
        return roomId;
    }

    public void setRoomId(String roomId) {
        this.roomId = roomId;
    }

    public List<StockListBean> getStockList() {
        return stockList;
    }

    public void setStockList(List<StockListBean> stockList) {
        this.stockList = stockList;
    }

    public static int getInventoryCount(List<LocatListBean> locatList) {
        if (locatList == null) {
            return 0;
        }
        int totalCount = 0;
        for (LocatListBean locatListBean : locatList) {
            List<StockListBean> stockList = locatListBean.getStockList();
            if (stockList == null) {
                continue;
            }
            int count = 0;
            for (StockListBean stockListBean : stockList) {
                String inventoryDate = stockListBean.getInventoryDate();
                if (TextUtils.isEmpty(inventoryDate)) {
                    continue;
                }
                count++;
            }
            totalCount += count;
        }
        return totalCount;
    }

    public int getStockNum(List<LocatListBean> locatList) {
        if (locatList == null) {
            return 0;
        }
        int totalCount = 0;
        for (LocatListBean locatListBean : locatList) {
            int storehouseLocatId = locatListBean.getStorehouseLocatId();
            Integer stockNum = locatListBean.getStockNum();
            if (storehouseLocatId == getStorehouseLocatId()) {
                continue;
            }
            if (stockNum != null) {
                totalCount += stockNum;
            }
        }
        return totalCount;
    }

    public int getStockNum(List<LocatListBean> locatList, boolean purchase) {
        if (locatList == null) {
            return 0;
        }
        int totalCount = 0;
        for (LocatListBean locatListBean : locatList) {
            int storehouseLocatId = locatListBean.getStorehouseLocatId();

            if (purchase) {
                //采购
                totalCount += locatListBean.getStorageNum();
            } else {
                Integer stockNum = locatListBean.getStockNum();
                if (stockNum != null) {
                    totalCount += stockNum;
                }
            }

        }
        return totalCount;
    }

    public static int getStockNumDefault(List<LocatListBean> locatList, int locatId) {
        if (locatList == null) {
            return 0;
        }
        for (LocatListBean locatListBean : locatList) {
            int storehouseLocatId = locatListBean.getStorehouseLocatId();
            if (storehouseLocatId == locatId) {
                Integer stockNum = locatListBean.getStockNum();
                if (stockNum != null) {
                    return stockNum;
                } else {
                    return 0;
                }
            }
        }
        return 0;
    }

    public static int getStockNum2(List<LocatListBean> locatList, int locatId) {
        if (locatList == null) {
            return 0;
        }
        int totalCount = 0;
        for (LocatListBean locatListBean : locatList) {
            int storehouseLocatId = locatListBean.getStorehouseLocatId();
            if (storehouseLocatId == locatId) {
                continue;
            }
            Integer stockNum = locatListBean.getStockNum();
            if (stockNum != null) {
                totalCount += stockNum;
            }
        }
        return totalCount;
    }

    public static int getStorageNum(List<LocatListBean> locatList) {
        if (locatList == null) {
            return 0;
        }
        int totalCount = 0;
        for (LocatListBean locatListBean : locatList) {
            totalCount += locatListBean.getStorageNum();
        }
        return totalCount;
    }

    public static int getStorageNum(List<LocatListBean> locatList, LocatListBean listBean) {
        if (locatList == null) {
            return 0;
        }
        int totalCount = 0;
        for (LocatListBean locatListBean : locatList) {
            if (locatListBean.getStorehouseLocatId() == listBean.getStorehouseLocatId()) {
                continue;
            }
            totalCount += locatListBean.getStorageNum();
        }
        return totalCount;
    }

    public static int getAddNum(List<LocatListBean> locatList, LocatListBean listBean) {
        if (locatList == null) {
            return 0;
        }
        int totalCount = 0;
        for (LocatListBean locatListBean : locatList) {
            if (locatListBean.getStorehouseLocatId().equals(listBean.getStorehouseLocatId())) {
                continue;
            }
            totalCount += locatListBean.getAddNum();
        }
        return totalCount;
    }

    public static int getStockNum(List<LocatListBean> locatList, LocatListBean listBean) {
        if (locatList == null) {
            return 0;
        }
        int totalCount = 0;
        for (LocatListBean locatListBean : locatList) {
            if (locatListBean.getStorehouseLocatId() == listBean.getStorehouseLocatId()) {
                continue;
            }
            Integer stockNum = locatListBean.getStockNum();
            if (stockNum != null) {
                totalCount += stockNum;
            }
        }
        return totalCount;
    }

    public static String getLocText(List<LocatListBean> locatList, String locationName) {
        if (locatList == null || locatList.size() == 0) {
            return locationName;
        }
        StringBuilder text = new StringBuilder();
        for (LocatListBean locatListBean : locatList) {
            if (locatListBean.getStorehouseLocatName() == null) {
                continue;
            }
            text.append(locatListBean.getStorehouseLocatName())
                    .append("(")
                    .append(locatListBean.getStorageNum())
                    .append(")");
        }
        return text.toString();
    }

    public static String getLocStockText(List<LocatListBean> locatList, String locationName) {
        if (locatList == null || locatList.size() == 0) {
            return locationName;
        }
        StringBuilder text = new StringBuilder();
        for (LocatListBean locatListBean : locatList) {
            List<ProductBean> productList = locatListBean.getProductList();
            Integer stockNum = locatListBean.getStockNum();
            if (stockNum == null) {
                if (productList != null) {
                    locatListBean.setStockNum(productList.size());
                } else {
                    locatListBean.setStockNum(0);
                }
            }
            text.append(locatListBean.getStorehouseLocatName())
                    .append("(")
                    .append((locatListBean.getStockNum() == null ? "0" : locatListBean.getStockNum()))
                    .append(")");
        }
        return text.toString();
    }

    public static boolean hasExists(List<LocatListBean> locatList, LocatListBean locatListBean) {
        if (locatListBean == null) {
            return false;
        }
        if (locatList == null || locatList.size() == 0) {
            return false;
        }
        for (LocatListBean bean : locatList) {
            if (bean.getStorehouseLocatId() == null) {
                continue;
            }
            if (bean.getStorehouseLocatId().equals(locatListBean.getStorehouseLocatId())) {
                return true;
            }
        }
        return false;
    }

    public Integer getLocationStockNum() {
        return locationStockNum;
    }

    public void setLocationStockNum(Integer locationStockNum) {
        this.locationStockNum = locationStockNum;
    }
}
